package hn.cch.spring.boot.controller;

import hn.cch.spring.boot.message.Request;
import hn.cch.spring.boot.message.Response;
import hn.cch.spring.boot.service.CookieService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


/**
 * cookie 是存储在浏览器上的字符串
 *
 */
@Controller
@RequestMapping("/cookie/")
public class CookieController {

    private final static Logger logger = LoggerFactory.getLogger(CookieController.class);

    @Autowired
    private CookieService cookieService;

    @RequestMapping("home")
    public void home(){
        logger.info("home");
    }


    @GetMapping("html")
    public String html(HttpServletResponse httpServletResponse){
        cookieService.createCookie(httpServletResponse);
        return "cookie/home";
    }


    @PostMapping("json")
    @ResponseBody
    public Response<String> json(@RequestBody Request request){
        logger.info("requestMessage:{}", request);
        Response<String> response = Response.success(UUID.randomUUID().toString());
        logger.info("response:{}", response);
        return response;
    }

}
